using System;
using System.Collections.Generic;
using System.Text;
using Domain.Entities;
using Domain.Services;
using System.Web;
using System.Security.Cryptography;
using System.Web.SessionState;
using UserInterface.Misc;
using UserInterface.MML.Base;

namespace UserInterface.MML
{
    public class QuoteTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            StringBuilder content = new StringBuilder();
            if (tagItem.Value != null)
            {
                int messageId;
                if (int.TryParse(tagItem.Value, out messageId))
                {
                    Message message = MessageService.GetById(messageId);
                    if (message != null)
                    {
                        string userName = message.Poster.Username;
                        HTMLTagBuilder link = new HTMLTagBuilder("a", userName + ":");
                        link.Attributes["href"] = "ViewTopic.aspx?topicId=" + message.Topic.Id + "&messageId=" + message.Id + "#message_" + messageId;
                        content.Append(link.BuildRegularTag());
                        content.Append("<br>");
                    }
                }
            }
            content.Append(GetCompiledContent(tagItem, context));
            HTMLTagBuilder container = new HTMLTagBuilder("div", content.ToString());
            container.Attributes["class"] = "quote";
            return container.BuildRegularTag();
        }
    }

    public class CodeTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            StringBuilder content = new StringBuilder();
            StringBuilder headerText = new StringBuilder();
            headerText.Append("Code");
            if (tagItem.Value != null)
            {
                headerText.Append(" (");
                headerText.Append(tagItem.Value);
                headerText.Append(")");
            }
            headerText.Append(":");

            HTMLTagBuilder header = new HTMLTagBuilder("h3", headerText.ToString());
            content.Append(header.BuildRegularTag());
            
            HTMLTagBuilder code = new HTMLTagBuilder("pre", GetOriginalContent(tagItem));
            content.Append(code.BuildRegularTag());

            HTMLTagBuilder container = new HTMLTagBuilder("div", content.ToString());
            container.Attributes["class"] = "code";
            return container.BuildRegularTag();
        }
    }

    public class SizeTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            if (tagItem.Value == null)
            {
                return null;
            }

            HTMLTagBuilder fontText = new HTMLTagBuilder("font", GetCompiledContent(tagItem, context));
            fontText.Attributes["style"] = "font-size:" + tagItem.Value + "pt";
            return fontText.BuildRegularTag();
        }
    }

    public class ImageTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            string tagContent = Compiler.GetOriginalContent(tagItem).Trim();
            
            string title = null;
            string src = null;
            if (tagItem.Attributes.ContainsKey("attachment"))
            {
                Message message = context as Message;
                if (message == null)
                {
                    return null;
                }

                int localId;
                if (!int.TryParse(tagItem.Attributes["attachment"].Value, out localId))
                {
                    return null;
                }

                Attachment attachment = message.GetAttachment(localId);
                if (attachment == null)
                {
                    return null;
                }

                src = attachment.Id == 0
                    ? "OutputTempAttachment.aspx?data=" + HttpUtility.UrlEncode(EncryptTools.Encrypt(attachment.FilePath))
                        + "&type=" + HttpUtility.UrlEncode(attachment.FileType)
                    : "OutputAttachment.aspx?id=" + attachment.Id;
                title = tagContent == String.Empty 
                    ? "Attachment: " + attachment.FileName + " (" + attachment.FileLength + " bytes)"
                    : tagContent;
            }
            else if (tagItem.Value == null)
            {
                src = tagContent.Trim();
                title = tagContent;
            }
            else
            {
                src = tagItem.Value.Trim();
                title = tagContent == String.Empty
                    ? tagItem.Value
                    : tagContent;
            }
            if (src == String.Empty)
            {
                return null;
            }

            HTMLTagBuilder imgTag = new HTMLTagBuilder("img");
            if (title != null)
            {
                imgTag.Attributes["title"] = title;
            }
            imgTag.Attributes["src"] = src;
            return imgTag.BuildSelfClosingTag();
        }
    }

    public class URLTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            if (tagItem.Value == null)
            {
                return null;
            }
            string href = tagItem.Value;

            string tagContent = GetOriginalContent(tagItem);
            tagContent = tagContent.Trim();
            string text = tagContent == String.Empty ? href : tagContent;

            HTMLTagBuilder link = new HTMLTagBuilder("a", text);
            link.Attributes["title"] = href;
            link.Attributes["href"] = href;
            return link.BuildRegularTag();
        }
    }

    public class SpoilerTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            string content = GetCompiledContent(tagItem, context);
            HTMLTagBuilder tag = new HTMLTagBuilder("div", content);
            tag.Attributes["class"] = "spoiler";
            return tag.BuildRegularTag();
        }
    }

    public class SearchTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            if (tagItem.Value == null)
            {
                return null;
            }
            string terms = tagItem.Value;
            string url = "Search.aspx?terms=" + HttpUtility.UrlEncode(terms);
            string tagContent = GetCompiledContent(tagItem, context).Trim();
            string content = tagContent == String.Empty
                ? "Search for messages with terms \"" + terms + "\""
                : tagContent;
            HTMLTagBuilder link = new HTMLTagBuilder("a", content);
            link.Attributes["href"] = url;
            return link.BuildRegularTag();
        }
    }

    public class GoogleTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            if (tagItem.Value == null)
            {
                return null;
            }
            string terms = tagItem.Value;
            string url = "http://www.google.nl/search?q=" + HttpUtility.UrlEncode(terms);
            string tagContent = GetCompiledContent(tagItem, context).Trim();
            string content = tagContent == String.Empty
                ? "Search Google with terms \"" + terms + "\""
                : tagContent;
            HTMLTagBuilder link = new HTMLTagBuilder("a", content);
            link.Attributes["href"] = url;
            return link.BuildRegularTag();
        }
    }

    public class AttachmentTagHandler : TagHandler
    {
        public override string Handle(TagItem tagItem, object context)
        {
            if (tagItem.Value == null)
            {
                return null;
            }
            
            Message message = context as Message;
            if (message == null)
            {
                return null;
            }

            int localId;
            if (!int.TryParse(tagItem.Value, out localId))
            {
                return null;
            }

            Attachment attachment = message.GetAttachment(localId);
            if (attachment == null)
            {
                return null;
            }

            string content = tagItem.IsSelfClosing
                ? "Attachment: " + attachment.FileName + " (" + attachment.FileLength + " bytes)"
                : GetCompiledContent(tagItem, context);
            string title = "Download attachment \"" + attachment.FileName + "\"";

            HTMLTagBuilder link = new HTMLTagBuilder("a", content);
            link.Attributes["href"] = UITools.getAttachmentDownloadURL(attachment);
            link.Attributes["title"] = title;
            return link.BuildRegularTag();
        }
    }
}
